package cn.zhao.controller;

import cn.zhao.entity.User;
import cn.zhao.service.impl.LoginService;
import cn.zhao.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/auth")
public class LoginController {

    @Autowired
    private LoginService loginService;

    /**
     * 登出
     * @param user
     * @return
     */
    @PostMapping("/login")
    public ResponseResult login(@RequestBody User user){
        return loginService.login(user);
    }

    /**
     * 登入
     * @return
     */
    @GetMapping("/logout")
    public ResponseResult logout(){
        return loginService.logout();
    }

    @GetMapping("/test")
    @PreAuthorize("hasAuthority('admin1')")
    public ResponseResult test(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        return new ResponseResult(200,"获取成功",authentication.getPrincipal());
    }
}
